package com.example.mall.service;

import com.example.mall.dao.AccountDao;
import com.example.mall.dao.PersonInfoDao;
import com.example.mall.dto.response.BaseResponse;
import com.example.mall.dto.response.UserResponse;
import com.example.mall.model.Account;
import com.example.mall.model.PersonInfo;
import com.example.mall.util.AESUtil;
import com.example.mall.util.SendMailThread;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;

@Service
public class LoginService {

    @Autowired
    private PersonInfoDao personInfoDao;
    @Autowired
    private AccountDao accountDao;

    /*
    public boolean signUP(User user)
    {
        return false;
    }
    */

    public UserResponse login(Integer id, String password, Integer flag, String captcha)
    {
        Account account = accountDao.getById(id);
        if (account != null)
        {
            if (!account.getFlag().equals(flag))
                return null;

            String dp_password = AESUtil.decrypt(account.getPassword(), "wuuuudlebegiare");
            String s1 = DigestUtils.md5DigestAsHex(dp_password.getBytes());
            //System.out.println(DigestUtils.md5DigestAsHex((user.getPassword()+captcha).getBytes()));
            if (DigestUtils.md5DigestAsHex((s1 + captcha).getBytes()).equals(password)) {
                UserResponse userResponse = new UserResponse();
                PersonInfo personInfo = personInfoDao.getById(account.getpID());
                userResponse.setId(personInfo.getId());
                userResponse.setUsername(account.getUsername());
                userResponse.setEmail(account.getEmail());
                userResponse.setFlag(account.getFlag());
                userResponse.setSex(personInfo.getSex());
                userResponse.setAvatl(personInfo.getAvatl());

                return userResponse;
            }
        }
        return null;
    }

    public void forgetPassword(String email, Integer flag)
    {
        Account account = accountDao.getByMailFlag(email, flag);
        if (account != null)
        {
            String dp_password = AESUtil.decrypt(account.getPassword(), "wuuuudlebegiare");
            SendMailThread sendMailThread = new SendMailThread(account.getEmail(),
                    "账号信息",
                    "账号：" + email + "\n" + "密码：" + dp_password);
            sendMailThread.start();
        }
    }

    public boolean register(String email, String password, Integer flag, String username, String idR, String name, Short sex)
    {
        Account account = new Account();
        account.setEmail(email);
        account.setPassword(AESUtil.encrypt(password, "wuuuudlebegiare"));
        account.setFlag(flag);
        account.setUsername(username);

        PersonInfo personInfo = new PersonInfo();
        personInfo.setSex(sex);
        personInfo.setIdR(idR);
        personInfo.setName(name);

        if (accountDao.getByMailFlag(email, flag) != null)
            return false;

        if (accountDao.getByMail(email).isEmpty()) {
            personInfoDao.insert(personInfo);
            Integer pID = personInfoDao.getByIdR(idR).getId();
            account.setpID(pID);
            accountDao.insert(account);
        }
        else {
            List<Account> accountList = accountDao.getByMail(email);
            Integer pID = accountList.get(0).getpID();
            account.setpID(pID);
            accountDao.insert(account);
        }

        return true;
    }

    public BaseResponse changePassword(String email, String oldPassword,Integer flag, String newPassword)
    {
        Account account = accountDao.getByMailFlag(email, flag);
        if (account == null)
            return BaseResponse.noLogin("未找到用户，请重新登录");
        String dp_string = AESUtil.decrypt(account.getPassword(), "wuuuudlebegiare");
        if (!oldPassword.equals(dp_string))
            return BaseResponse.badrequest("原密码错误");
        account.setPassword(AESUtil.encrypt(newPassword, "wuuuudlebegiare"));
        int result = accountDao.updatePasswordByEmail(account);
        if (result == 1)
            return BaseResponse.success("更改密码成功,请重新登录");
        return BaseResponse.noLogin("未知错误，请重新登录");
    }


}
